﻿/* 
 * Godtear.com
 * QQ:bzure@189.cn
 *Author:Nathan Liu
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Bzure.Common.Configuration.AppConfiguration;

namespace Bzure.Common.Configuration.AppConfiguration
{
    public abstract class ReflectConfigSection : ConfigurationSection
    {
        public ReflectConfigSection()
        {
        }
        public Object[] Params { get; set; }
        public string ComponentType
        {
            get
            {
                return this.GetProperty("type");
            }
        }
        public bool Single
        {
            get
            {
                string re = this.GetProperty("single");
                if (string.IsNullOrEmpty(re))
                    return false;
                if (re == "true" || re == "True")
                    return true;
                return false;
            }
        }
        public override void Load(XmlNode node)
        {
            XmlAttributeCollection attributes = node.Attributes;
            if (attributes["params"] != null && attributes["paramsType"] != null)
            {
                string Paramters = attributes["params"].Value;
                string ParamsType = attributes["paramsType"].Value;
                string[] ps = Paramters.Split(',');
                string[] pts = ParamsType.Split(',');
                if (ps.Length == pts.Length)
                {
                    Params = new object[ps.Length];
                    try
                    {
                        for (int i = 0; i < ps.Length; i++)
                        {
                            Type type = Type.GetType(pts[i]);
                            Params[i] = Convert.ChangeType(ps[i], type, null);
                        }
                    }
                    catch
                    {
                        this.Params = null;
                    }
                }
            }
            base.Load(node);
        }
    }

}
